package com.bluemartini.jmx.callback;

import com.bluemartini.dna.BMException;
import com.bluemartini.dna.BMLog;
import com.bluemartini.dna.Callback;
import com.bluemartini.dna.DNAList;
import com.bluemartini.dna.DNAListIterator;
import com.bluemartini.jmx.BMMBeanServer;
import com.bluemartini.jmx.mbean.BusinessAction;
import com.bluemartini.server.BMClient;
import com.bluemartini.server.BusinessActionDictionary;

/**
 * Register as MBean the cache list returned by HTMLVersionUtil.getCacheStats(). <br/>
 * Add this class to the startup callback section.
 * 
 * @author Yannick Robin
 *
 */
public class BusinessActionMBeanRegister implements Callback {

	public DNAList execute(DNAList dnaIn) throws BMException
	{
		registerBusinessActionsMBeans();
		return null;
	}
	
	private void registerBusinessActionsMBeans() throws BMException
	{
		BMLog.log(BMLog.COMPONENT_SYSTEM, 1, "[CacheMBeanRegisterCallback.registerBusinessActionsMBeans] Refresh Business Action MBeans");
		
		BMClient.enableStats();
		DNAListIterator iter = BusinessActionDictionary.alphaIterator();
		
		String name;
		while ((name = iter.nextName()) != null)
		{		
			BusinessAction mbean = new BusinessAction(name);
	        //semicolon is not support for Object Name
	        name = name.replace(':', '_');		        
	        BMMBeanServer.registerMBean(BMMBeanServer.MBEAN_DOMAIN_NAME + ":Name=" + name + ",Type=BusinessAction", mbean);
		}
	}
	
}
